package cn.cvs.controller;

import cn.cvs.pojo.SysUser;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * 示例3
 * 第一个Spring MVC控制器
 *
 * 示例7：修改控制器
 *
 * 示例8：@RequestMapper注解
 */
@Controller
@RequestMapping("/hello")
public class HelloController {


    Logger logger = Logger.getLogger(HelloController.class);

    @RequestMapping("/hello")
    public ModelAndView hello(@RequestParam String realName) throws Exception{

        logger.debug("你好【"+realName+"】欢迎来到Spring MVC课堂！");
        //跳转到hello页面
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("realName",realName);
        modelAndView.setViewName("hello");
        return modelAndView;
    }

    @RequestMapping("/hello2")
    public String hello2(Model model, @RequestParam String realName) throws Exception{

        logger.debug("你好【"+realName+"】欢迎来到Spring MVC课堂！");
        model.addAttribute("realName",realName);
        model.addAttribute(realName);
        SysUser sysUser =new SysUser();
        sysUser.setRealName(realName);
        model.addAttribute("currentUser",sysUser);
        model.addAttribute(sysUser);
        //跳转到hello页面
        return "hello";
    }

    @RequestMapping("hello3")
    public String hello3(Map<String,Object> map,
                         @RequestParam String realName)throws Exception{
        logger.debug("你好【"+realName+"】欢迎来到Spring MVC课堂！");
        map.put("realName",realName);
        SysUser sysUser =new SysUser();
        sysUser.setRealName(realName);
        map.put("currentUser",sysUser);
        map.put("sysUser",sysUser);
        return "hello";
    }
}
